package com.evaluation.kexingcp.psycho.evaluation.sys.llm

import com.evaluation.kexingcp.common.security.annotation.Inner
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.body
import reactor.core.publisher.Flux


data class LlmRequest(val model:String? = "lite", val messages: List<Map<String,String>>? = null, val stream: Boolean? = true)

@Configuration
open class LlmWebClientConfig{

    @Bean
    open fun xfyunAiClient(): WebClient {
        return WebClient.builder()
            .baseUrl("https://spark-api-open.xf-yun.com/v1")
            .defaultHeader("Authorization", "Bearer kThaJPeMKGuOmDmgTSee:bBUzoNZWZxBsxZEwWbdD")
            .defaultHeader("Content-Type", "application/json")
            .build()
    }
}

@RestController
@RequestMapping("/llm")
open class LlmController(
    private val xfyunAiClient: WebClient
) {
    @PostMapping("/xfyun", produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
    @Inner(false)
    open fun xfyun(@RequestBody request: LlmRequest): Flux<String> = xfyunAiClient.post()
        .uri("/chat/completions")
        .bodyValue(request)
        .retrieve()
        .bodyToFlux(String::class.java)
}